<?php

/*
	Dažniausiai užduodami klausimai
*/

/**
 * Sukuriame mysql lentele jei jos nera
 *
 */
function duk() {
	$db->uzklausa("CREATE TABLE IF NOT EXISTS `duk` (
	`id` INT NULL AUTO_INCREMENT PRIMARY KEY ,
	`klausimas` VARCHAR( 200 ) NOT NULL ,
	`atsakymas` TEXT NOT NULL ,
	`vieta` INT( 5 ) NULL DEFAULT '1',
	INDEX ( `klausimas` ) ,
	FULLTEXT (
	`atsakymas`
	)
	) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_lithuanian_ci;");
	$db->uzklausa("INSERT INTO `duk` ( `klausimas` , `atsakymas` , `vieta` ) VALUES ('Kas yra D.U.K.?', 'Tai yra trumpinys nuo žodžių - &quot;Dažniausiai užduodami klausimai&quot;', '1');");
	
	klaida("Klaida","Atsiprašome - puslapis kuriamas");
	die();
}


/**
 * Atvaizdavimas
 */
if (defined("LEVEL")) { lentele("Apie - ".$conf['Pavadinimas'],$conf['Apie']); }
$text = ''; $extra="<ol>"; $nr = 0;
$sql = mysql_query("SELECT * FROM duk ORDER by `vieta` ASC"); if (mysql_errno() == 1146) { duk(); }
if (mysql_num_rows($sql) > 0) {
	while ($row = mysql_fetch_assoc($sql)) {
		$nr++;
		$extra .= "<li><a href='#".$row['id']."'>".$row['klausimas']."</a></li>\n";
		$text .= "<p><div class='title'><b>".$nr.".</b> ".(defined("LEVEL") && LEVEL >= 20?"<a href='".url("d,".(int)$row['id']."")."'><img src='images/icons/bullet_delete.png' class='middle' alt='delete' border='0' /></a><a href='".url("e,".(int)$row['id']."")."'><img src='images/icons/bullet_green.png' class='middle' alt='delete' border='0' /></a>":"")."<a name='".$row['id']."'>".$row['klausimas']."</a></div>\n<div class='sarasas'>".$row['atsakymas']."</div></p>\n";
		$last_id = $row['id'];
	}
}

/**
* Pabaiga
*/

//Administravimas

if (isset($_GET) && !empty($_GET) && defined("LEVEL") && LEVEL >= 20) { 
	//Jei adminas bando trinti. $_GET['d'] == ID
	if (isset($_GET['d']) && isnum($_GET['d']) && $_GET['d'] > 0) {
		$q = "DELETE FROM `duk` WHERE `id` = ".escape((int)$_GET['d']);
		$db->uzklausa($q) or die(klaida("klaida","ajajaj"));
		redirect("?id,8","header");
		//$value = "Trinti";
	}
	elseif (isset($_GET['n']) || isset($_GET['e'])) { ?>
		<script language="javascript" type="text/javascript" src="javascript/htmlarea/tiny_mce/tiny_mce.js"></script>
		<script language="JavaScript">
		// Notice: The simple theme does not use all options some of them are limited to the advanced theme
		tinyMCE.init({
			mode : "exact",
			elements : "Atsakymas",
			theme : "simple",
			apply_source_formatting : true
		});
		</script>
		<?
			$klausimas = '';
			$atsakymas = '';
			$order = $last_id+1;
		
		//Jeigu adminas nori redaguoti
		// url['e'] pasakome koki ID redaguosime
		if (isset($_GET['e']) && isnum($_GET['e']) && $_GET['e'] > 0) {
			$value = "Keisti"; $id = ceil((int)$_GET['e']); if ($id < 0) { $id = 0; }
			$sql = mysql_fetch_assoc($db->uzklausa("SELECT * FROM `duk` WHERE `id` = ".escape($id)." LIMIT 1")) or klaida(mysql_error());
			$klausimas = $sql['klausimas'];
			$atsakymas = $sql['atsakymas'];
			$order = (int)$sql['vieta'];
		}
		
		
		//Jei adminas bando rasyti nauja DUK
		elseif (isset($_GET['n']) && $_GET['n'] == 1) {
			$value = "Įrašyti";
		}
		
		$duk = array(
			"Form"=>array("action"=>"","method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"duk"),
			"Klausimas:"=>array("type"=>"text","value"=>input($klausimas),"name"=>"Klausimas","style"=>"width:400px"),
			"Atsakymas:"=>array("type"=>"textarea","value"=>input($atsakymas),"name"=>"Atsakymas","id"=>"Atsakymas","class"=>"input","rows"=>"10","style"=>"width:400px"),
			"Vieta (order by):"=>array("type"=>"text","value"=>input((int)$order),"name"=>"Order","style"=>"width:200px"),
			""=>array("type"=>"hidden","value"=>input($id),"name"=>"id","id"=>"id"),
			""=>array("type"=>"submit","name"=>"duk","value"=>$value)
		);
		
		include_once("dievai/class.php");
		$bla = new forma();
		lentele("Naujas klausimas/atsakymas",$bla->form($duk,"D.U.K. rašymas/koregavimas"));
		
	}
}

if (isset($_POST) && !empty($_POST) && isset($_POST['duk']) && defined("LEVEL") && LEVEL >= 20) {
	//apsauga nuo kenksmingo kodo
	include_once('priedai/safe_html.php');
	// nurodome masyva leidziamu elementu naujienoje
	// - tagai kurie uzdaromi atskirai (<p></p>) pazymeti kaip 1
	// - tagai kuriuos uzdaryti nebutina (<hr>) zymimi kaip 0
	$tags= array ( "p"=>1, "br"=>0, "a"=>1, "img"=>0,
						"li"=>1, "ol"=>1, "ul"=>1,
						"b"=>1, "i"=>1, "em"=>1, "strong"=>1,
						"del"=>1, "ins"=>1, "u"=>1, "code"=>1, "pre"=>1,
						"blockquote"=>1, "hr"=>0, "span"=>1, "font"=>1,"h1"=>1,"h2"=>1,"h3"=>1,
						"table"=>1, "tr"=>1, "td"=>1, "th"=>1,"tbody"=>1, "div"=>1
						);
	
	$klausimas = safe_html($_POST['Klausimas'], $tags );
	$atsakymas = safe_html($_POST['Atsakymas'], $tags );
	$order = ceil((int)$_POST['Order']);
	$id = ceil((int)$url['e']);

	//jeigu rasom nauja
	if ($_POST['duk'] == 'Įrašyti') {
		$q = "INSERT INTO `duk` (`klausimas`,`atsakymas`,`vieta`) VALUES (
		".escape($klausimas).",
		".escape($atsakymas).",
		".escape($order).");";
		$db->uzklausa($q) or die (mysql_error());
		redirect("?id,8","header");
	}
	
	//jeigu redaguojam
	elseif ($_POST['duk'] == 'Keisti') {
		$q = "UPDATE `duk` SET
		`atsakymas` = ".escape($_POST['Atsakymas']).",
		`klausimas` = ".escape($_POST['Klausimas']).",
		`vieta` = ".escape((int)$_POST['Order'])." WHERE `id`=".$id." LIMIT 1 ;";
		$db->uzklausa($q) or die (mysql_error());
		redirect("?id,8","header");
	}
	
	//jeigu kazkas netaip
	else {
		klaida("Kas per velnias?","Tai jau čia kažką netaip padarei. Mėgink dar kartą");
	}
}

//Atspauzdinam vaizda
lentele("Klausimai",$extra."</ol>".(defined("LEVEL") && LEVEL >=20?"<a href='?id,8;n,1' class='button'>Rašyti naują</a>":""));
lentele("Atsakymai",$text."");
unset($extra,$text);
?>